package com.michael.demos.base.leetcode;

/**
 * 类功能描述:
 * <pre>
 *   xxxx
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2020/9/23 11:30
 */
public class Solution59 {

    public static void main(String[] args) {
        int size = 4;
        int[][] matrix = generateMatrix(size);
        for (int i = 0; i < matrix.length; i++) {
            int[] row = matrix[i];
            System.out.print("");
            for (int j = 0; j < row.length; j++) {
                if (j == 0) {
                    System.out.print("[");
                }
                System.out.print(row[j]);
                if (j < row.length - 1) {
                    System.out.print(",");
                } else {
                    System.out.println("]");
                }
            }
        }
    }

    public static int[][] generateMatrix(int n) {
        int[][] arr = new int[n][n];
        int c = 1, j = 0;
        // 一圈一轮回
        while (c <= n * n) {
            // 上横
            for (int i = j; i < n - j; i++) {
                arr[j][i] = c++;
            }
            // 右竖
            for (int i = j + 1; i < n - j; i++) {
                arr[i][n - j - 1] = c++;
            }
            // 下横
            for (int i = n - j - 2; i >= j; i--) {
                arr[n - j - 1][i] = c++;
            }
            // 左竖
            for (int i = n - j - 2; i > j; i--) {
                arr[i][j] = c++;
            }
            j++;
        }
        return arr;
    }
}
